package com.fany.utils;

import android.graphics.Bitmap;
import android.os.AsyncTask;
import android.widget.ImageView;
import android.widget.ListView;

import java.lang.ref.WeakReference;

/**
 * Created by FANY on 2015/1/31.
 */
public class BitmapThumbnailTask extends AsyncTask<Object, Integer, Bitmap>
{
    private final WeakReference<ImageView> _imageViewReference;
    private String _imageViewTag;

    public BitmapThumbnailTask(ImageView view)
    {
        _imageViewReference = new WeakReference(view);
    }

    @Override
    protected Bitmap doInBackground(Object[] objects)
    {
        String filePath = objects[0].toString();
        _imageViewTag = filePath;
        int pixels = Integer.valueOf(objects[1].toString());
        return BitmapHelper.extractThumbnail(filePath, pixels);
    }

    @Override
    protected void onPostExecute(Bitmap bitmap)
    {
        if (_imageViewReference != null && bitmap != null)
        {
            final ImageView imageView = _imageViewReference.get();
            if ((imageView != null) && imageView.getTag().toString().equals(_imageViewTag))
            {
                imageView.setImageBitmap(bitmap);
            }
        }
    }
}
